package com.ha.security.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.GregorianCalendar;
import java.util.List;

@Service
public class UserSetalService implements UserDetailsService {


    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       //基于数据库的查询查询用户数据
        //User user=userMapper.selectUserByUsername(username);

        if (!"jake".equals(username)) {
            throw  new UsernameNotFoundException("用户不存在");
        }

        String encodePwd=passwordEncoder.encode("111");

        List<GrantedAuthority> grantedAuthorities
                = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_admin,ROLE_normal,sys:res:retrieve,sys:res:create");
        //spring提供的用户校验
        User user=new User(username, encodePwd,grantedAuthorities);


        return user;
    }
}
